package fatworm.concept.datatype;
/**
 * data type DATETIME
 */
public class FatDateTime extends fatworm.concept.DataType{
	private static final FatDateTime instance = new FatDateTime();
	public static FatDateTime getInstance(){
		return instance;
	}
	
	private FatDateTime(){}
	private java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public DateTimeValue convert( SqlValue value ) throws ConvertException {
		switch( value.getType().getTypeKey() ){
			case T_STRING:
				try{
					return new DateTimeValue( dateFormat.parse( value.toString()));
				}catch( java.text.ParseException e){
					throw new ConvertException( value , this );
				}
			case T_DATETIME: return (DateTimeValue)value;
			default		: throw new ConvertException( value , this );
		}
	} 
	
	public int getTypeKey() {
		return T_DATETIME;
	}
	
	public String toString() {
		return "DATETIME";
	}	
	
	public class DateTimeValue extends SqlValue{
		public java.util.Date value;
		private DateTimeValue( java.util.Date value ){
			this.value = value;
		}
		public String toString(){
			return  dateFormat.format(value);
		}
		public int compareTo(DateTimeValue o) {
			return value.compareTo( o.value );
		}
	}
}
